<?php

namespace App\Http\Controllers\Kms\App\SelfCheck;

use App\Http\Controllers\BaseController;
use App\Http\Validate\Kms\SelfCheck\SelfCheckValidate;
use App\Service\Cruise\Mission\CheckInputService;
use App\Service\Kms\SelfCheck\SelfCheckReportService;
use App\Service\Kms\SelfCheck\SelfCheckService;
use Illuminate\Http\JsonResponse;
use App\Support\CustomForm\AttributeCruiseSupport;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Tinderbox\Clickhouse\Exceptions\ClientException;

class SelfCheckController extends BaseController
{
    /**
     * 自检报告详情
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function detail(Request $request, int $id)
    {
        return $this->success(SelfCheckReportService::detail($id, auth()->user()->rmsUserId, 'kms'));
    }

    /**
     * 督导审核局部更新
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ValidationException
     */
    public function auditSave(Request $request, int $id)
    {
        $this->validate($request, [
            'field'                   => 'required|string',
            'value'                   => 'required|integer|in:1,2',  // 合格/不合格
            'instructions' => 'required_if:value,' . AttributeCruiseSupport::CRUISE_CHECK_FAIL . '|string', // 情况说明
        ], [
            'instructions.required_if' => '您有必填项未填写',
        ]);

        $value = (int)$request->get('value');
        $instructions = (string)$request->get('instructions');

        (new CheckInputService($id, auth()->user()->rmsUserId))->selfCheckAuditSave($request->field, $value, $instructions);

        return $this->success([]);
    }

    /**
     * 督导审核提交
     * @param Request $request
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function auditSubmit(Request $request, int $id)
    {
        (new CheckInputService($id, auth()->user()->rmsUserId))->selfCheckAuditSubmit();

        return $this->success();
    }


    /**
     * 门店自检数据统计看板
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface|ClientException
     */
    public function  groupStatusStatistical(Request $request): JsonResponse
    {
        $params = SelfCheckValidate::groupStatusStatisticalValidate($request->all());
        return $this->success(SelfCheckService::groupStatusStatistical($params));
    }

    /**
     * 门店自检数据统计看板 - 门店列表
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface|ClientException
     */
    public function  groupStatusStatisticalShop(Request $request): JsonResponse
    {
        $params = SelfCheckValidate::groupStatusStatisticalShopValidate($request->all());
        return $this->success(SelfCheckService::groupStatusStatisticalShop($params));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function selfCheckReportLists(Request $request): JsonResponse
    {
        $params = SelfCheckValidate::selfCheckReportListsValidate($request->all());
        return $this->success(SelfCheckService::selfCheckReportLists($params));
    }

    /**
     * 门店列表
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     * @throws ClientException
     */
    public function shopLists(Request $request): JsonResponse
    {
        $params = SelfCheckValidate::shopListsValidate($request->all());
        return $this->success(SelfCheckService::shopLists($params));
    }

    /**
     * 自检任务详情 不含表单信息
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function selfCheckDetail(Request $request): JsonResponse
    {
        $params = SelfCheckValidate::selfCheckDetailValidate($request->all());
        return $this->success(SelfCheckService::selfCheckDetail($params));
    }
}
